package farmers.base64coder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Base64Coder {
    @Test
    public void base64Encoder() throws UnsupportedEncodingException {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        jsonObject1.put("first", "上海第一有限公司688888000001");
        jsonObject2.put("second", "上海第二有限公司688888000002");
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);

        String encode = URLEncoder.encode(jsonArray.toJSONString(), "UTF-8");
        System.out.println(encode);
    }

    @Test
    public void base64Decoder() throws UnsupportedEncodingException {
        String str = "%5B%7B%22first%22%3A%22%E4%B8%8A%E6%B5%B7%E7%AC%AC%E4%B8%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8688888000001%22%7D%2C%7B%22second%22%3A%22%E4%B8%8A%E6%B5%B7%E7%AC%AC%E4%BA%8C%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8688888000002%22%7D%5D";
        String decode = URLDecoder.decode(str, "UTF-8");
        System.out.println(decode);
    }
}
